package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.VivoglyDao;
import com.bdqn.zhang.dao.impl.dbutil.DButil;
import com.bdqn.zhang.pojo.Memberinfo;
import com.bdqn.zhang.pojo.Vivoej;
import com.bdqn.zhang.pojo.Vivogly;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VivoglyDaoimpl extends DButil implements VivoglyDao {
    Connection conn = null;
    PreparedStatement ment = null;
    ResultSet resultSet = null;

    @Override
    public List<Vivogly> getAll(int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivogly> list = new ArrayList();
        String sql = "select * from Vivo_gly limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, from);
            ment.setObject(2, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivogly vivogly = new Vivogly();
                    vivogly.setId(resultSet.getString(1));
                    vivogly.setName(resultSet.getString(2));
                    vivogly.setPassword(resultSet.getString(3));
                    vivogly.setZhuangtai(resultSet.getInt(4));
                    vivogly.setPhoto(resultSet.getString(5));
                    vivogly.setSex(resultSet.getString(6));
                    vivogly.setPhone(resultSet.getString(7));
                    list.add(vivogly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int getCount() {
        ResultSet resultSet = null;
        PreparedStatement ment = null;
        conn = getConnerction();
        String sql = "select count(1) from Vivo_gly";
        try {
            ment = conn.prepareStatement(sql);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }


    @Override
    public int gai(Vivogly vivogly) {
        String sql = "UPDATE Vivo_gly SET zhuangtai=? WHERE id=?";
        Object[] obj = {vivogly.getZhuangtai(), vivogly.getId()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int del(String id) {
        String sql = "delete from Vivo_gly where id in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }

    @Override
    public int add(Vivogly vivogly) {
        String sql = "INSERT INTO Vivo_gly(name,password,sex,photo,zhuangtai,phone) VALUES(?,?,?,?,?,?)";
        Object[] obj = {vivogly.getName(), vivogly.getPassword(), vivogly.getSex(), vivogly.getPhoto(), vivogly.getZhuangtai(),vivogly.getPhone()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int edits(Vivogly vivogly) {
        String sql = "UPDATE Vivo_gly SET name=?,zhuangtai=?,photo=?,sex=?,phone=? WHERE id=?";
        Object[] obj = {vivogly.getName(),vivogly.getZhuangtai(), vivogly.getPhoto(), vivogly.getSex(),vivogly.getPhone(),vivogly.getId()};
        return executeUpdate(sql, obj);
    }

    @Override
    public List<Vivogly> dan(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivogly> list = new ArrayList();
        String sql = "select * from Vivo_gly where concat(id,name,sex) like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivogly vivogly = new Vivogly();
                    vivogly.setId(resultSet.getString(1));
                    vivogly.setName(resultSet.getString(2));
                    vivogly.setPassword(resultSet.getString(3));
                    vivogly.setZhuangtai(resultSet.getInt(4));
                    vivogly.setPhoto(resultSet.getString(5));
                    vivogly.setSex(resultSet.getString(6));
vivogly.setPhone(resultSet.getString(7));
                    list.add(vivogly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int mhdan(String name) {
        conn = getConnerction();
        String sql = "select count(1) from Vivo_gly where concat(id,name,sex) like ?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, "%" + name + "%");
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Vivogly> ztcx(int name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivogly> list = new ArrayList();
        String sql = "select * from Vivo_gly where zhuangtai=? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, name);
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivogly vivogly = new Vivogly();
                    vivogly.setId(resultSet.getString(1));
                    vivogly.setName(resultSet.getString(2));
                    vivogly.setPassword(resultSet.getString(3));
                    vivogly.setZhuangtai(resultSet.getInt(4));
                    vivogly.setPhoto(resultSet.getString(5));
                    vivogly.setSex(resultSet.getString(6));
                    vivogly.setPhone(resultSet.getString(7));
                    list.add(vivogly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }
    @Override
    public int mhztcxs(int name) {
        conn = getConnerction();
        String sql = "select count(1) from Vivo_gly where zhuangtai=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }


    @Override
    public int mhztcx(int name) {
        conn = getConnerction();
        String sql = "select count(1) from Vivo_gly where concat(id,name,sex) like =?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int dlcx(String yh, String mm) {
        conn = getConnerction();
        String sql = "select count(1) from Vivo_gly where name=? and password=? and zhuangtai=1";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, yh);
            ment.setObject(2, mm);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public String ctx(String name) {
        conn = getConnerction();
        String sql = "select photo from Vivo_gly where name=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getString(1);
                }
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int checkNameUnique(String name) {
        conn = getConnerction();
        String sql = "select count(1) from Vivo_gly where name=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Vivogly> czl(String name) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivogly> list = new ArrayList();
        String sql = "select * from Vivo_gly where name=?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivogly vivogly = new Vivogly();
                    vivogly.setId(resultSet.getString(1));
                    vivogly.setName(resultSet.getString(2));
                    vivogly.setPassword(resultSet.getString(3));
                    vivogly.setZhuangtai(resultSet.getInt(4));
                    vivogly.setPhoto(resultSet.getString(5));
                    vivogly.setSex(resultSet.getString(6));
                    vivogly.setPhone(resultSet.getString(7));
                    list.add(vivogly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Vivogly> yhmcx(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivogly> list = new ArrayList();
        String sql = "select * from Vivo_gly where name like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivogly vivogly = new Vivogly();
                    vivogly.setId(resultSet.getString(1));
                    vivogly.setName(resultSet.getString(2));
                    vivogly.setPassword(resultSet.getString(3));
                    vivogly.setZhuangtai(resultSet.getInt(4));
                    vivogly.setPhoto(resultSet.getString(5));
                    vivogly.setSex(resultSet.getString(6));
                    vivogly.setPhone(resultSet.getString(7));
                    list.add(vivogly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Vivogly> xbcx(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivogly> list = new ArrayList();
        String sql = "select * from Vivo_gly where sex=? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, name);
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivogly vivogly = new Vivogly();
                    vivogly.setId(resultSet.getString(1));
                    vivogly.setName(resultSet.getString(2));
                    vivogly.setPassword(resultSet.getString(3));
                    vivogly.setZhuangtai(resultSet.getInt(4));
                    vivogly.setPhoto(resultSet.getString(5));
                    vivogly.setSex(resultSet.getString(6));
                    vivogly.setPhone(resultSet.getString(7));
                    list.add(vivogly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }
}
